package com.silverbox.waimai.thread;

import com.silverbox.core.util.Utils;
import com.silverbox.shopper.dubboApi.info.ShopperInfoServiceApi;
import com.silverbox.shopper.entity.DiningArea;
import com.silverbox.shopper.entity.EpayShopConfig;
import com.silverbox.shopper.entity.ShopperConfig;
import com.silverbox.sweeporder.common.Constants;
import com.silverbox.sweeporder.core.ApplicationBeanContext;
import com.silverbox.sweeporder.service.webmsg.WebMsgService;
import com.silverbox.waimai.entity.Waimai;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * <pre>
 * 功    能:
 * 涉及版本:
 * 创 建 者: YangYangjun
 * 日    期: 2020-07-01 10:22
 * </pre>
 */
public class SendMsgToMachineThread implements Runnable{

    private final static Logger logger = LoggerFactory.getLogger(SendMsgToMachineThread.class);

    private ShopperInfoServiceApi shopperInfoServiceApi;
    private WebMsgService webMsgService;
    private Waimai waimai;

    public SendMsgToMachineThread(Waimai waimai) {
        this.shopperInfoServiceApi = ApplicationBeanContext.getBean(ShopperInfoServiceApi.class);
        this.webMsgService = ApplicationBeanContext.getBean(WebMsgService.class);
        this.waimai = waimai;
    }

    @Override
    public void run() {
        Map<String, Object> shopperConfig = shopperInfoServiceApi.getShopperAllConfig(waimai.getShopId());
        Integer aliyunMqtt = (null == shopperConfig.get("CTR_ALIYUN_MQTT") || Utils.isEmpty(shopperConfig.get("CTR_ALIYUN_MQTT").toString())) ? 0 : Integer.valueOf(shopperConfig.get("CTR_ALIYUN_MQTT").toString());
        Long hostMachineId = null;
        Integer onlineDataBackflow = (null == shopperConfig.get("ONLINE_ORDER_DATA_BACKFLOW") || Utils.isEmpty(shopperConfig.get("ONLINE_ORDER_DATA_BACKFLOW").toString())) ? 0 : Integer.valueOf(shopperConfig.get("ONLINE_ORDER_DATA_BACKFLOW").toString());

        if (new Integer(1).equals(onlineDataBackflow)) {
            // 获取数据回流主机Id
            EpayShopConfig epayShopConfig = shopperInfoServiceApi.getEpayShopConfig(waimai.getShopId());
            if (epayShopConfig != null) {
                hostMachineId = epayShopConfig.getMachineId();
            }
        }

        //获取推送类型 在线&外卖 = 2 ，桌台 = 1
        int msgType = getSendMsgTypeByWaimai(waimai);
        DiningArea tableInfo = null;
        if (!Utils.isEmpty(waimai.getTableNum()) && !Utils.isEmpty(waimai.getTableUid())) {
            tableInfo = shopperInfoServiceApi.getDiningAreaByUid(waimai.getTableUid());
        }
        // 配置了秒付数据回流后，推送商家消息至主机
        logger.info("payNotify->sendWebMsg:" + msgType + "," + aliyunMqtt + ","
                + hostMachineId);

        String msgCode = Utils.createOrderNum(waimai.getShopId());
        webMsgService.sendWebMsg(waimai, (tableInfo == null ? null : tableInfo.getMachineId()), msgType,
                aliyunMqtt, hostMachineId,msgCode);

        // add by ywc 2019年11月27日 	增加下单消息保存
        ShopperConfig sweepPayPrintObj = shopperInfoServiceApi.getShopperConfig(waimai.getShopId(), "CTR_SWEEP_PAY_PRINT");
        Integer sweepPayPrint = sweepPayPrintObj == null ? 0 : Integer.valueOf(sweepPayPrintObj.getPropValue());
        webMsgService.saveAddWaimaiMsgRecord(sweepPayPrint, waimai, tableInfo, hostMachineId, msgCode);
    }

    /*
     *<pre>
     * 说    明: 获取推送消息类型
     * 涉及版本:
     * 创 建 者: YangYangjun
     * 日    期: 2020-06-28 16:03
     *</pre>
     **/
    private int getSendMsgTypeByWaimai(Waimai waimai){
        Integer clientOrderMode = Integer.parseInt(waimai.getOuterSupportId().toString());
        int msgType;
        if(Constants.clientOrderMode.ONLINE_ORDER_MODE.getValue().equals(clientOrderMode)
                || Constants.clientOrderMode.WAIMAI_ORDER_MODE.getValue().equals(clientOrderMode)){
            msgType = 2;
        }else {
            msgType = 1;
        }
        return msgType;
    }
}
